package se.gala.games.quetzalcoatl;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.ListCell;
import javafx.scene.control.SplitPane;
import javafx.scene.control.TextField;
import javafx.util.StringConverter;
import se.gala.users.BalanceUpdateObserver;
import se.gala.users.PlayerBalanceObservable;
import se.gala.utils.GGObject;
import se.gala.utils.MoneyInt;

public class UIQController extends GGObject implements BalanceUpdateObserver {
	
	@FXML protected GemButton b00;
	@FXML protected GemButton b01;
	@FXML protected GemButton b02;
	@FXML protected GemButton b03;
	@FXML protected GemButton b04;
	@FXML protected GemButton b05;
	@FXML protected GemButton b06;
	@FXML protected GemButton b07;
	
	@FXML protected GemButton b10;
	@FXML protected GemButton b11;
	@FXML protected GemButton b12;
	@FXML protected GemButton b13;
	@FXML protected GemButton b14;
	@FXML protected GemButton b15;
	@FXML protected GemButton b16;
	@FXML protected GemButton b17;
	
	@FXML protected GemButton b20;
	@FXML protected GemButton b21;
	@FXML protected GemButton b22;
	@FXML protected GemButton b23;
	@FXML protected GemButton b24;
	@FXML protected GemButton b25;
	@FXML protected GemButton b26;
	@FXML protected GemButton b27;
	
	@FXML protected GemButton b30;
	@FXML protected GemButton b31;
	@FXML protected GemButton b32;
	@FXML protected GemButton b33;
	@FXML protected GemButton b34;
	@FXML protected GemButton b35;
	@FXML protected GemButton b36;
	@FXML protected GemButton b37;
	
	@FXML protected GemButton b40;
	@FXML protected GemButton b41;
	@FXML protected GemButton b42;
	@FXML protected GemButton b43;
	@FXML protected GemButton b44;
	@FXML protected GemButton b45;
	@FXML protected GemButton b46;
	@FXML protected GemButton b47;
	
	@FXML protected GemButton b50;
	@FXML protected GemButton b51;
	@FXML protected GemButton b52;
	@FXML protected GemButton b53;
	@FXML protected GemButton b54;
	@FXML protected GemButton b55;
	@FXML protected GemButton b56;
	@FXML protected GemButton b57;
	
	@FXML protected GemButton b60;
	@FXML protected GemButton b61;
	@FXML protected GemButton b62;
	@FXML protected GemButton b63;
	@FXML protected GemButton b64;
	@FXML protected GemButton b65;
	@FXML protected GemButton b66;
	@FXML protected GemButton b67;
	
	@FXML protected GemButton b70;
	@FXML protected GemButton b71;
	@FXML protected GemButton b72;
	@FXML protected GemButton b73;
	@FXML protected GemButton b74;
	@FXML protected GemButton b75;
	@FXML protected GemButton b76;
	@FXML protected GemButton b77;
	
	@FXML protected SplitPane screenSplit;

	@FXML protected TextField fldBalance;
	@FXML protected ComboBox<BetBoxItem> boxBet;
	
	@FXML protected Button btnReshuffle;
	@FXML protected Button btnRandom;
	@FXML protected Button btnRandomMove;
	@FXML protected Button btnBooster;
	
	Balance balance;
	
	@FXML protected void initialize() {
		UIQuetzalcoatl.uiqc = this;
		registerGemButtons();
		balance = UIQuetzalcoatl.getBalance();
		balance.addObserver(this);
		updateBalance();
		setBetBox();						
	}
	
	private void setBetBox() {
		ObservableList<BetBoxItem> betBoxData = FXCollections.observableArrayList();
		betBoxData.add(new BetBoxItem("10p", UIQuetzalcoatl.getBalance().moneyIntFactory(10)));
		betBoxData.add(new BetBoxItem("20p", UIQuetzalcoatl.getBalance().moneyIntFactory(20)));
		betBoxData.add(new BetBoxItem("50p", UIQuetzalcoatl.getBalance().moneyIntFactory(50)));
		betBoxData.add(new BetBoxItem("£1", UIQuetzalcoatl.getBalance().moneyIntFactory(100)));
		betBoxData.add(new BetBoxItem("£2", UIQuetzalcoatl.getBalance().moneyIntFactory(200)));
		betBoxData.add(new BetBoxItem("£5", UIQuetzalcoatl.getBalance().moneyIntFactory(500)));
		betBoxData.add(new BetBoxItem("£10", UIQuetzalcoatl.getBalance().moneyIntFactory(1000)));
		boxBet.setItems(betBoxData);
				
		boxBet.setCellFactory((comboBox) -> {
		    return new ListCell<BetBoxItem>() {
		        @Override
		        protected void updateItem(BetBoxItem item, boolean empty) {
		            super.updateItem(item, empty);

		            if (item == null || empty) {
		                setText(null);
		            } else {
		                setText(item.key);
		            }
		        }
		    };
		});
		boxBet.setConverter(new StringConverter<BetBoxItem>() {
		    @Override
		    public String toString(BetBoxItem item) {
		        if (item == null) {
		            return null;
		        } else {
		            return item.key;
		        }
		    }

		    @Override
		    public BetBoxItem fromString(String key) {
		        return null; // No conversion fromString needed.
		    }
		});
		
		for (BetBoxItem b : boxBet.getItems()) {
			if (b.value.getCentValueAsInt() == UIQuetzalcoatl.stakeAmount.getCentValueAsInt()) {
				boxBet.getSelectionModel().select(b);
				break;
			}
		}
		
		boxBet.setOnAction((event) -> {
			BetBoxItem selection = boxBet.getSelectionModel().getSelectedItem();
			UIQuetzalcoatl.stakeAmount = selection.value;
			doRestart(event);
		});
	}
		
	private void updateBalance() {
		fldBalance.setText(balance.getRealCashBalance().getValueAsCurrencyFormattedString());
	}

	@FXML protected void doRandom(ActionEvent event) {
		UIQuetzalcoatl.doRandom();
	}

	@FXML protected void doMove(ActionEvent event) {
		if (UIQuetzalcoatl.getGame().checkForLegalMoves()) {
			//UIQuetzalcoatl.doRandomMove();
			UIQuetzalcoatl.doBestMove();
		} else {
			doReshuffle(null);
		}
	}

	@FXML protected void doReshuffle(ActionEvent event) {
		UIQuetzalcoatl.doReshuffle();
	}

	@FXML protected void doStoneCrash(ActionEvent event) {
		UIQuetzalcoatl.doStoneCrash();
	}

	@FXML protected void doRestart(ActionEvent event) {
		UIQuetzalcoatl.doRestart();
	}

	@Override
	public void update(PlayerBalanceObservable o, MoneyInt difference) {
		updateBalance();
	}
	
	private void registerGemButtons() {
		UIQuetzalcoatl.register(b00);
		UIQuetzalcoatl.register(b01);
		UIQuetzalcoatl.register(b02);
		UIQuetzalcoatl.register(b03);
		UIQuetzalcoatl.register(b04);
		UIQuetzalcoatl.register(b05);
		UIQuetzalcoatl.register(b06);
		UIQuetzalcoatl.register(b07);
		UIQuetzalcoatl.register(b10);
		UIQuetzalcoatl.register(b11);
		UIQuetzalcoatl.register(b12);
		UIQuetzalcoatl.register(b13);
		UIQuetzalcoatl.register(b14);
		UIQuetzalcoatl.register(b15);
		UIQuetzalcoatl.register(b16);
		UIQuetzalcoatl.register(b17);
		UIQuetzalcoatl.register(b20);
		UIQuetzalcoatl.register(b21);
		UIQuetzalcoatl.register(b22);
		UIQuetzalcoatl.register(b23);
		UIQuetzalcoatl.register(b24);
		UIQuetzalcoatl.register(b25);
		UIQuetzalcoatl.register(b26);
		UIQuetzalcoatl.register(b27);
		UIQuetzalcoatl.register(b30);
		UIQuetzalcoatl.register(b31);
		UIQuetzalcoatl.register(b32);
		UIQuetzalcoatl.register(b33);
		UIQuetzalcoatl.register(b34);
		UIQuetzalcoatl.register(b35);
		UIQuetzalcoatl.register(b36);
		UIQuetzalcoatl.register(b37);
		UIQuetzalcoatl.register(b40);
		UIQuetzalcoatl.register(b41);
		UIQuetzalcoatl.register(b42);
		UIQuetzalcoatl.register(b43);
		UIQuetzalcoatl.register(b44);
		UIQuetzalcoatl.register(b45);
		UIQuetzalcoatl.register(b46);
		UIQuetzalcoatl.register(b47);
		UIQuetzalcoatl.register(b50);
		UIQuetzalcoatl.register(b51);
		UIQuetzalcoatl.register(b52);
		UIQuetzalcoatl.register(b53);
		UIQuetzalcoatl.register(b54);
		UIQuetzalcoatl.register(b55);
		UIQuetzalcoatl.register(b56);
		UIQuetzalcoatl.register(b57);
		UIQuetzalcoatl.register(b60);
		UIQuetzalcoatl.register(b61);
		UIQuetzalcoatl.register(b62);
		UIQuetzalcoatl.register(b63);
		UIQuetzalcoatl.register(b64);
		UIQuetzalcoatl.register(b65);
		UIQuetzalcoatl.register(b66);
		UIQuetzalcoatl.register(b67);
		UIQuetzalcoatl.register(b70);
		UIQuetzalcoatl.register(b71);
		UIQuetzalcoatl.register(b72);
		UIQuetzalcoatl.register(b73);
		UIQuetzalcoatl.register(b74);
		UIQuetzalcoatl.register(b75);
		UIQuetzalcoatl.register(b76);
		UIQuetzalcoatl.register(b77);
	}
}

class BetBoxItem {
	final String key;
	final MoneyInt value;
	
	public BetBoxItem(String key, MoneyInt value) {
		this.key = key;
		this.value = value;
	}
}
